<template>
  <div class="pagination">
    <button :disabled="currentPageNum===1" @click="$emit('changeNum',currentPageNum - 1)">上一页</button>
    <button v-if="startEnd.start>1" @click="$emit('changeNum',1)">1</button>
    <button v-if="startEnd.start>2">···</button>

    <button @click="$emit('changeNum',page)" :class="{active:page===currentPageNum} "
            v-for="(page,index) in startEnd.end" :key="page" v-if="page>startEnd.start">{{ page }}
    </button>


    <button v-if="startEnd.end<totalPage-1">···</button>
    <button v-if="startEnd.end<totalPage" @click="$emit('changeNum',totalPage)">{{ totalPage }}</button>
    <button :disabled="currentPageNum===totalPage" @click="$emit('changeNum',currentPageNum+1)">下一页</button>

    <button style="margin-left: 30px">共 {{ total }}条</button>
  </div>
</template>

<script>
export default {
  name: "Pagination",
  props: {
    //当前页
    currentPageNum: {
      type: Number,
      required: true
    },
    //总条数
    total: {
      type: Number,
      default: 0
    },
    //每页多少条
    pageSize: {
      type: Number,
      required: true
    },
    //连续数
    continueNum: {
      type: Number,
      default: 5
    }
  },
  computed: {
    //计算总页数
    totalPage() {
      return Math.ceil(this.total / this.pageSize)
    },
    //根据当前页的不同计算连续也的起始位置和结束位置
    startEnd() {
      let {currentPageNum, total, continueNum, pageSize, totalPage} = this
      let start = 0, end = 0
      if (continueNum > currentPageNum) {
        start = 1,
            end = continueNum
      } else {
        //先考虑正常情况下
        start = currentPageNum - Math.floor(continueNum / 2)
        end = currentPageNum + Math.floor(continueNum / 2)
        //大于最大值 小于最小值
        if (start < 1) {
          start = 1,
              end = continueNum
        }
        if (end > totalPage) {
          start = totalPage - continueNum + 1,
              end = totalPage
        }
      }
      return {start, end}

    }
  }
}
</script>

<style lang="less" scoped>
.pagination {
  button {
    margin: 0 5px;
    background-color: #f4f4f5;
    color: #606266;
    outline: none;
    border-radius: 2px;
    padding: 0 4px;
    vertical-align: top;
    display: inline-block;
    font-size: 13px;
    min-width: 35.5px;
    height: 28px;
    line-height: 28px;
    cursor: pointer;
    box-sizing: border-box;
    text-align: center;
    border: 0;

    &[disabled] {
      color: #c0c4cc;
      cursor: not-allowed;
    }

    &.active {
      cursor: not-allowed;
      background-color: #409eff;
      color: #fff;
    }
  }
}
</style>
<template>
  <div class="pagination">
    <button :disabled="currentPageNum===1" @click="$emit('changeNum',currentPageNum - 1)">上一页</button>
    <button v-if="startEnd.start>1" @click="$emit('changeNum',1)">1</button>
    <button v-if="startEnd.start>2">···</button>

    <button @click="$emit('changeNum',page)" :class="{active:page===currentPageNum} "
            v-for="(page,index) in startEnd.end" :key="page" v-if="page>startEnd.start">{{ page }}
    </button>


    <button v-if="startEnd.end<totalPage-1">···</button>
    <button v-if="startEnd.end<totalPage" @click="$emit('changeNum',totalPage)">{{ totalPage }}</button>
    <button :disabled="currentPageNum===totalPage" @click="$emit('changeNum',currentPageNum+1)">下一页</button>

    <button style="margin-left: 30px">共 {{ total }}条</button>
  </div>
</template>

<script>
export default {
  name: "Pagination",
  props: {
    //当前页
    currentPageNum: {
      type: Number,
      required: true
    },
    //总条数
    total: {
      type: Number,
      default: 0
    },
    //每页多少条
    pageSize: {
      type: Number,
      required: true
    },
    //连续数
    continueNum: {
      type: Number,
      default: 5
    }
  },
  computed: {
    //计算总页数
    totalPage() {
      return Math.ceil(this.total / this.pageSize)
    },
    //根据当前页的不同计算连续也的起始位置和结束位置
    startEnd() {
      let {currentPageNum, total, continueNum, pageSize, totalPage} = this
      let start = 0, end = 0
      if (continueNum > currentPageNum) {
        start = 1,
            end = continueNum
      } else {
        //先考虑正常情况下
        start = currentPageNum - Math.floor(continueNum / 2)
        end = currentPageNum + Math.floor(continueNum / 2)
        //大于最大值 小于最小值
        if (start < 1) {
          start = 1,
              end = continueNum
        }
        if (end > totalPage) {
          start = totalPage - continueNum + 1,
              end = totalPage
        }
      }
      return {start, end}

    }
  }
}
</script>

<style lang="less" scoped>
.pagination {
  button {
    margin: 0 5px;
    background-color: #f4f4f5;
    color: #606266;
    outline: none;
    border-radius: 2px;
    padding: 0 4px;
    vertical-align: top;
    display: inline-block;
    font-size: 13px;
    min-width: 35.5px;
    height: 28px;
    line-height: 28px;
    cursor: pointer;
    box-sizing: border-box;
    text-align: center;
    border: 0;

    &[disabled] {
      color: #c0c4cc;
      cursor: not-allowed;
    }

    &.active {
      cursor: not-allowed;
      background-color: #409eff;
      color: #fff;
    }
  }
}
</style>
<template>
  <div class="pagination">
    <button :disabled="currentPageNum===1" @click="$emit('changeNum',currentPageNum - 1)">上一页</button>
<!--  这个1不是一直存在的,需要根据计算出来的连续数是不是1决定的  -->
    <button v-if="startEnd.start!==1" @click="$emit('changeNum',1)">1</button>
    <button v-if="startEnd.start>2">···</button>
<!--v-for和v-if同时使用的时候 v-for优先级高-->
    <button @click="$emit('changeNum',page)" :class="{active:page===currentPageNum} "
            v-for="(page,index) in startEnd.end" :key="page" v-if="page>=startEnd.start">{{ page }}
    </button>


    <button v-if="startEnd.end<totalPage-1">···</button>
    <button v-if="startEnd.end<totalPage" @click="$emit('changeNum',totalPage)">{{ totalPage }}</button>
    <button :disabled="currentPageNum===totalPage" @click="$emit('changeNum',currentPageNum+1)">下一页</button>

    <button style="margin-left: 30px">共 {{ total }}条</button>
  </div>
</template>

<script>
export default {
  name: "Pagination",
  props: {
    //当前页
    currentPageNum: {
      type: Number,
      required: true
    },
    //总条数
    total: {
      type: Number,
      default: 0
    },
    //每页多少条
    pageSize: {
      type: Number,
      required: true
    },
    //连续数
    continueNum: {
      type: Number,
      default: 5
    }
  },
  computed: {
    //计算总页数
    totalPage() {
      return Math.ceil(this.total / this.pageSize)
    },
    //根据当前页的不同计算连续也的起始位置和结束位置
    startEnd() {
      let {currentPageNum, total, continueNum, pageSize, totalPage} = this
      let start = 0, end = 0
      if (continueNum > currentPageNum) {
        start = 1,
            end = continueNum
      } else {
        //先考虑正常情况下
        start = currentPageNum - Math.floor(continueNum / 2)
        end = currentPageNum + Math.floor(continueNum / 2)
        //大于最大值 小于最小值
        if (start < 1) {
          start = 1,
              end = continueNum
        }
        if (end > totalPage) {
          start = totalPage - continueNum + 1,
              end = totalPage
        }
      }
      return {start, end}

    }
  }
}
</script>

<style lang="less" scoped>
.pagination {
  button {
    margin: 0 5px;
    background-color: #f4f4f5;
    color: #606266;
    outline: none;
    border-radius: 2px;
    padding: 0 4px;
    vertical-align: top;
    display: inline-block;
    font-size: 13px;
    min-width: 35.5px;
    height: 28px;
    line-height: 28px;
    cursor: pointer;
    box-sizing: border-box;
    text-align: center;
    border: 0;

    &[disabled] {
      color: #c0c4cc;
      cursor: not-allowed;
    }

    &.active {
      cursor: not-allowed;
      background-color: #409eff;
      color: #fff;
    }
  }
}
</style>
